package com.sword.controller.finance;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.finance.Receivables;
import com.sword.pojo.finance.ReceivablesPay;
import com.sword.service.finance.ReceivablesPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.shiro.SecurityUtils;

@Controller
@RequestMapping("/finance")
public class ReceivablesPayController {
    @Autowired
    private ReceivablesPayService ReceivablesPayService;

    @RequestMapping("/receivablesPay")
    public String receivablesPay() { return "views/finance/receivablespay"; }
    /**
     * 查询功能
     */
    @RequestMapping("/queryReceivablesPay")
    @ResponseBody
    public PageResult queryReceivablesPay(@RequestBody QueryPageBean queryPageBean) {
        try {
            return ReceivablesPayService.queryReceivablesPay(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_RECEIVABLESPAY_FAIL, null, null);
        }
    }

    /*
     * 添加功能
     */
    @ResponseBody
    @RequestMapping("/addReceivablesPay")
    public Result addReceivablesPay(@RequestBody ReceivablesPay receivablesPay) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            receivablesPay.setCreateid(userId);
            receivablesPay.setStatus(0);
            // 判断是否添加成功
            Integer flag = ReceivablesPayService.addReceivablesPay(receivablesPay);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_RECEIVABLESPAY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_RECEIVABLESPAY_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_RECEIVABLESPAY_SUCCESS, null);
    }

    @ResponseBody
    @RequestMapping("editReceivablesPay")
    public Result editReceivablesPay(@RequestBody ReceivablesPay receivablesPay) {
        try {
            Integer flag = ReceivablesPayService.editReceivablesPay(receivablesPay);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLESPAY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLESPAY_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLESPAY_SUCCESS, null);
    }

    /**
     * 删除商品总分类
     */

    @ResponseBody
    @RequestMapping("deleteReceivablesPay")
    public Result deleteReceivablesPay(Integer id) {
        try {
            Integer flag = ReceivablesPayService.deleteReceivablesPay(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_RECEIVABLESPAY_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_RECEIVABLESPAY_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_RECEIVABLESPAY_SUCCESS, null);
    }
    @ResponseBody
    @RequestMapping("exBuyorderDetail")
    public Result examineBuyorderDetail(@RequestBody ReceivablesPay receivablesPay) {
        try {
            Integer flag = ReceivablesPayService.examineBuyorderDetail(receivablesPay);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLES_SUCCESS, null);
    }
    /**
     * 弃审退货明细
     */
    @ResponseBody
    @RequestMapping("unexBuyorderDetail")
    public Result unexamineBuyorderDetail(@RequestBody ReceivablesPay receivablesPay) {
        try {
            Integer flag = ReceivablesPayService.unexamineBuyorderDetail(receivablesPay);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_RECEIVABLES_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_RECEIVABLES_SUCCESS, null);
    }
}
